package com.hlg.xboot.model.sys;

import com.hlg.xboot.enums.Status;
import com.hlg.xboot.model.BaseModel;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import java.util.List;

/**
 * Created by tzw on 2017-06-06.
 */

@Entity
@DynamicUpdate
@DynamicInsert
public class SysResource extends BaseModel implements Comparable<SysResource> {
    /**
     * 资源名称
     */
    @NotEmpty(message = "{SysResource.name.NotEmpty}")
    @Column(nullable = false,length = 20)
    private String name;

    /**
     * 资源类型（url,botton,method）
     */
    @Column(nullable = false,length = 1)
    private Integer type;

    /**
     * 资源图标
     */
    private String icon;

    /**
     * 资源url
     */
    private String url;

    /**
     * 权限编码
     */
    private String code;

    /**
     * 排序标识
     */
    private Integer sort;

    /**
     * 启动状态
     * 0:启用
     * 1：禁用
     */
    private Integer states = Status.ENABLED.getValue();

    /**
     * 资源描述
     */
    private String description;

    /**
     * 上级菜单
     */
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "parentid")
    private SysResource parent;

    /**
     * 角色
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinTable(name = "sys_role_resource",
            joinColumns = @JoinColumn(name = "resource_id", referencedColumnName = "id", nullable = false)
            , inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false))
    private List<SysRole> roles;

    public SysResource(){

    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getUrl() {
        return url;
    }

    public Integer getStates() {
        return states;
    }

    public void setStates(Integer states) {
        this.states = states;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public SysResource getParent() {
        return parent;
    }

    public void setParent(SysResource parent) {
        this.parent = parent;
    }


    @Override
    public int compareTo(SysResource o) {
        return this.getSort().compareTo(o.getSort());
    }
}
